package eerbaar.v2.gamebuilder.server;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import eerbaar.game.IGame;
import eerbaar.server.gamebuilder.Builder;
import eerbaar.shared.exceptions.GameException;
import eerbaar.shared.persist.ISerializerService;
import eerbaar.shared.service.ServiceFactory;
import eerbaar.v2.gamebuilder.GamePacket;

@SuppressWarnings("serial")
public class GameServiceImpl extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {

        try {

            String key = request.getHeader("key");
            String action = request.getHeader("action");
            if ("getGame".equalsIgnoreCase(action)) {

                GamePacket packet = getGame(key);
                PrintWriter writer = response.getWriter();
                writer.append(packet.getData());

            } else if ("nextLevel".equalsIgnoreCase(action)) {

                BufferedReader reader = request.getReader();
                String line = reader.readLine();
                ISerializerService service = ServiceFactory
                        .getService(ISerializerService.class);
                IGame game = service.load(IGame.class, line);
                GamePacket old = new GamePacket();
                old.setGame(game);

                GamePacket packet = this.nextLevel(key, old);

                PrintWriter writer = response.getWriter();
                writer.append(packet.getData());

            } else if ("resetGame".equalsIgnoreCase(action)) {

                BufferedReader reader = request.getReader();
                String line = reader.readLine();
                ISerializerService service = ServiceFactory
                        .getService(ISerializerService.class);
                IGame game = service.load(IGame.class, line);
                GamePacket old = new GamePacket();
                old.setGame(game);

                GamePacket packet = this.resetGame(key, old);

                PrintWriter writer = response.getWriter();
                writer.append(packet.getData());
            }

        } catch (GameException e) {
            e.printStackTrace();
        }
    }

    private GamePacket getGame(String cookie) throws GameException {

        IGame game = SERVER.loadGame(cookie);

        GamePacket result = new GamePacket();
        result.setGame(game);

        return result;
    }

    private GamePacket nextLevel(String key, GamePacket packet)
            throws GameException {

        GamePacket result = new GamePacket();
        IGame game = SERVER.unpackageGame(packet);

        game = new Builder().nextLevel(game);
        result.setGame(game);

        SERVER.saveGame(key, game);

        return result;
    }

    private GamePacket resetGame(String key, GamePacket packet)
            throws GameException {

        GamePacket result = new GamePacket();
        IGame game = SERVER.unpackageGame(packet);

        game = new Builder().buildGame();
        result.setGame(game);

        SERVER.saveGame(key, game);

        return result;
    }

}
